<?php

namespace app\index\controller;

use app\common\YujisoController;

class Storesku extends YujisoController
{
    public function __construct(){
        parent::__construct();
    }
    public function getList()
    {
       $list= \app\index\service\StoreskuService::getList($this->param);
        exitJson($list);
    }
    
    public function edit()
    {
        $param = $this->request->param();
        if(empty($param)){
             returnJson(0,'参数不能为空');
         }
        if ($this->request->isPost()) {   
            $id=$param['id'];
            unset($param['id']);
            foreach($param as $key=>$value){
                $rules[$key]='require';  
            }
            unset($param['s']);
            $param['operator']=CN_NAME;
            $this->validateData($rules); 
            \app\common\model\ToolModel::updateData(['id' => $id],$param,'store_sku');
            returnJson(1,'编辑成功');
        } else {      
            $user = \app\common\model\ToolModel::findByAttributes('store_sku', ['id' => $param['id']]);
            returnJson(1, '', ['list' => $user]);
        }
    }
     //启用和禁用
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        \app\common\model\ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'store_sku');
        returnJson(1,'处理成功');
    }
    public function add()
    {
        $rules=[];
         $param = $this->request->param();
         if(empty($param)){
             returnJson(0,'参数不能为空');
         }
         unset($param['id']);
        foreach($param as $key=>$value){
            $rules[$key]='require';  
        }
        $this->validateData($rules); 
        unset($param['s']);
        $param['update_time']=date('Y-m-d H:i:s');
        $param['operator']=CN_NAME;
        $one=\app\common\model\ToolModel::findByAttributes('store_sku',['sku'=>$param['sku'],'shop'=>$param['shop']]);
        if($one){
            returnJson(0,'添加失败，SKU已经存在'); 
        }
        \app\common\model\ToolModel::insertData($param,'store_sku');
        returnJson(1,'添加成功'); 
    }
    public function batchOperator()
    {
        $type = $this->request->param('batchType');
        $param=[];
        $param['title']=['店铺名称','SKU','销售状态','生命周期','sku等级'];//title
        $param['actionAsync']='\app\index\service\StoreskuService::uploadHandle';//涓婁紶鏁版嵁鐨勬暟鎹洖璋冨埌service灞傚鐞�
        $param['type']='\app\index\service\StoreskuService::uploadHandle';//涓婁紶鏁版嵁鐨勬暟鎹洖璋冨埌service灞傚鐞�
        $param['batchType']=$type;         
        $res=\app\common\service\ExcelLibraryService::read($param);
        exitJson($res);
    }
    public function getSkuName(){
        $list=[];
        $sku = $this->request->param('sku');
        if(is_string($sku)){
            $sku=[$sku];
        }
        $return=\app\common\service\SkuService::getSkuBySku($sku);
        if($return){
            foreach($return as $key=>$value){
              $list[] = $value;
            }
        }
        returnJson(1,'',$list);   
    }
    public function getCountryName(){
        $name = $this->request->param('name');
        $condition=[];
        $condition['name_zh']=['like',$name.'%'];
        $list = \app\common\model\ToolModel::findAllByWhere('base_country',$condition,'id,name_zh');
        returnJson(1,'',$list);   
    }
}

